<template>
  <!-- 仓库定义 -->
  <div>
    <TopFilter :list="topFilterList" :collapse="false" @filterChange="handleTopFilterChange"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="partPartInfoManageWarehouseDefinitionIndex"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visible" :title="dialogTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <BrandModal :type="handleType" :rowData="formData" @close="closeHandler" @refeshTableData="refeshTableData" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { getStorage } from '@/api/part/Warehouse';
import BrandModal from './brandModal';

export default {
  name: 'Brand',
  components: {
    BrandModal
  },
  mixins: [dictionary],
  data() {
    this.conf = { add: '新增', edit: '编辑', show: '显示' };
    return {
      topFilterList: this.createTopFilterList(),
      fetchParams: { xhrAbort: false },
      visible: false,
      dialogTitle: '',
      fetchApi: getStorage,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {}
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'workshopTag',
          itemList: this.createDictList('1004')
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 60,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button disabled={props.row.oemTag == '10451002'} size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '仓库代码',
          dataIndex: 'storageCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '仓库名称',
          dataIndex: 'storageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '仓库类型',
          dataIndex: 'storageType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('5000'),
          render: props => {
            return <span>{this.createDictText(props.row.storageType, '5000')}</span>;
          }
        },
        {
          title: '是否有效',
          dataIndex: 'workshopTag',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.workshopTag, '1004')}</span>;
          }
        },
        {
          title: '数据来源',
          dataIndex: 'oemTag',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1045', [10451003]),
          render: props => {
            return <span>{this.createDictText(props.row.oemTag, '1045')}</span>;
          }
        },
        {
          title: '停用时间',
          dataIndex: 'stopDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '计价方式',
          dataIndex: 'pricingMethod',
          sorter: true,
          render: props => {
            return <span>{this.createDictText(props.row.pricingMethod, '8085')}</span>;
          }
        }
      ];
    },
    // 点击搜索重置
    handleTopFilterChange(val) {
      this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign(this.fetchParams, val, {
        xhrAbort: false
      });
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    showHandler() {
      this.dialogTitle = `${this.conf[this.handleType]}仓库`;
      this.visible = true;
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    // 刷新主页面列表
    refeshTableData() {
      // this.fetchParams.xhrAbort = true;
      // this.fetchParams.xhrAbort = false;
    }
  }
};
</script>

<style lang="less" scoped></style>
